package com.yjhb.android.sbz.activity;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.adapter.BiddingAdapter;
import com.yjhb.android.sbz.entity.BiddingUser;
import com.yjhb.android.sbz.mvp.bidding.BiddingMorePresenter;
import com.yjhb.android.sbz.mvp.bidding.BiddingMoreView;
import com.jojo.android.zxlib.tool.ZxToast;
import com.jojo.android.zxlib.view.EmptyRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/12/9 9:45
 */
public class BiddingMoreActivity extends BaseActivity<BiddingMorePresenter> implements OnRefreshLoadmoreListener, BiddingMoreView {
    private final String TAG = "BiddingMoreActivity";

    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerView)
    EmptyRecyclerView recyclerView;

    BiddingAdapter biddingAdapter;
    List<BiddingUser> users = new ArrayList<>();

    private final int size = 25;
    private int current = 1;

    @Override
    protected BiddingMorePresenter initPresenter() {
        return new BiddingMorePresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bidding_more;
    }

    @Override
    protected void initUI() {
        recyclerView.setEmptyViewById(R.id.layout_empty);
        recyclerView.setAdapter(biddingAdapter = new BiddingAdapter(this));
        biddingAdapter.setData(users);
    }

    @Override
    protected void initData() {
        loadingDialog.show();
        current = 1;
        presenter.getBidList(size, current);
    }

    @Override
    protected void regEvent() {
        smartRefreshLayout.setOnRefreshLoadmoreListener(this);
    }

    @OnClick(R.id.back)
    public void back() {
        finish();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        current++;
        presenter.getBidList(size, current);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        current = 1;
        presenter.getBidList(size, current);
    }

    @Override
    public void loadMore(List<BiddingUser> biddingUsers) {
        users.addAll(biddingUsers);
        smartRefreshLayout.finishLoadmore();
        biddingAdapter.notifyDataSetChanged();
    }

    @Override
    public void refreshList(List<BiddingUser> biddingUsers) {
        loadingDialog.cancel();
        this.users.clear();
        users.addAll(biddingUsers);
        smartRefreshLayout.finishRefresh();
        biddingAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(int code, String msg) {
        smartRefreshLayout.finishRefresh();
        smartRefreshLayout.finishLoadmore();
        loadingDialog.cancel();
        if (code == 686) {
            ZxToast.showINFOBottomToast(msg);
        }
    }
}
